package com.esms.service.impl;

import com.esms.dao.ResponsibilityMapper;
import com.esms.po.Responsibility;
import com.esms.po.ResponsibilityMent;
import com.esms.service.IResponsibilityService;
import com.esms.vo.*;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class ResponsibilityServiceImpl implements IResponsibilityService {
    @Autowired
    ResponsibilityMapper responsibilityMapper;

    public List<EmpResponsVO> getDepartmentRespInfo(HashMap<String, Object> map){
        return responsibilityMapper.selectDepartmentRespInfo(map);
    }

    public List<EmpResponsVO> selectPositionRespInfo(HashMap<String, Object> map){
        return responsibilityMapper.selectPositionRespInfo(map);
    }

    public List<Responsibility> findEmpJobInfo(HashMap<String, Object> map){
        return responsibilityMapper.findEmpJobInfo(map);
    }

    public ResponsibilityMent selectGwzzinfoByAccount(HashMap<String, Object> map){
        return responsibilityMapper.selectGwzzinfoByAccount(map);
    }

    @Override
    public void updatePosResp(Integer rId, String content) {
        responsibilityMapper.updateRContentByid(rId,content);
    }

    @Override
    public void AddDepResp(ResponsibilityDepVo responsibilityVo) {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(ResponsibilityDepVo.class, Responsibility.class)
                .field("depId", "rDepartment")
                .field("content", "rContent")
                .byDefault().register();
        MapperFacade facade = mapperFactory.getMapperFacade();
        Responsibility responsibility = facade.map(responsibilityVo, Responsibility.class);
        responsibility.setrType("2");
        responsibility.setrJob(0);
        responsibilityMapper.insertResp(responsibility);

    }

    @Override
    public void updateResp(ResponsibilityChangeVo responsibilityChangeVo) {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(ResponsibilityChangeVo.class, Responsibility.class)
                .field("id", "rId")
                .field("depId", "rDepartment")
                .field("jobId", "rJob")
                .field("content", "rContent")
                .byDefault().register();
        MapperFacade facade = mapperFactory.getMapperFacade();
        Responsibility responsibility = facade.map(responsibilityChangeVo, Responsibility.class);
        responsibilityMapper.updateById(responsibility);
    }

    @Override
    public boolean checkIfExistExcluedId(Integer depId, Integer jobId, Integer id) {
        if (responsibilityMapper.existExcluedId(depId, jobId,id) != null) {
            return true;
        }
        return false;
    }
}
